package com.caowei.weicimpress.weicimpress;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.io.FilenameFilter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by caoweiPc on 2014/7/19.
 */
public class SysFileHandler extends AbstractFileHandler {

     private static final String TAG=SysFileHandler.class.getSimpleName();

    public SysFileHandler(Context context) {
        super(context);
    }

    @Override
    public MainPartFile subCompress(File file, String passwd, int compressmethod_key, List<FileInfoBean> beans, int subsize, String unit) {
        return null;
    }


    /*根文件是目录  说明操作的是系统文件*/
    @Override
    public boolean isThisHandler(File rootFile) {
        if(Utils.DEBUG){
            Log.d(TAG,"exe isThisHandler");
        }
        return rootFile.isDirectory();
    }


    @Override
    protected File subCompress(File saveFlie, String passwd, int compressmethod_key, List<FileInfoBean> beans) {
        return null;
    }


    @Override
    public File extractAll(File targetDir,FileInfoBean bean) {
        if(Utils.DEBUG){
            Log.d(TAG,"exe extract");
        }
        //todo  空方法
        return null;
    }

    @Override
    public File extractItem(File targetDir, FileInfoBean bean) {
        return null;
    }

    @Override
    public boolean isEncried(File file) {
        return false;
    }


    @Override
    public MyDataList listfiles( FileInfoBean fileInfoBean) {
        if(Utils.DEBUG){
            Log.d(TAG," exe list");
        }
        final File operDir=TextUtils.isEmpty(fileInfoBean.getParentPath())?
                fileInfoBean.getRootFile():
                new File(fileInfoBean.getParentPath());
        return gainResult(operDir);
    }

    @Override
    public boolean isSurpportCryptogram() {
        return false;
    }

    @Override
    public boolean isSplite(File rootFile) {
        return false;
    }

    @Override
    public boolean delete(List<FileInfoBean> bean,char[] passwd) {

        return false;
    }

    @Override
    public void batchRenamePartsByMainFile(MainPartFile mainPartFile) {

    }

    private MyDataList gainResult(final File operDir){
        final List<FileInfoBean> list=new ArrayList<FileInfoBean>();
        final MyDataList myDataList=new MyDataList(list);
        operDir.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String filename) {
                File file=new File(dir,filename);
                FileInfoBean bean = new FileInfoBean();
                bean.setDir(file.isDirectory());
                bean.setFileName(file.getName());
                bean.setParentPath(Utils.normalizePath(file.getParent()));
                bean.setRootFile(operDir);
                bean.setLastModFileTime(file.lastModified());
                //todo 暂定
                if(bean.isDir()){
                    bean.setIcoId(R.drawable.diricocontent);
                          /*  Runnable r= new Runnable(){
                                @Override
                                public void run() {
                                    float num=Utils.getFileSize(file);
                                }
                            };*/
                    int numOfDirs= myDataList.getNumOfDirs()==-1?0:myDataList.getNumOfDirs();
                    myDataList.setNumOfDirs(++numOfDirs);
                    String[] files= file.list();
                    bean.setNumOfSubFile(files==null?0:files.length);
                }else{


                    int numOfFiles= myDataList.getNumOfFiles()==-1?0:myDataList.getNumOfFiles();
                    myDataList.setNumOfFiles(++numOfFiles);
                    bean.setSize(BigDecimal.valueOf(file.length()));
                    bean.setIcoId(R.drawable.fileico);

                    /*FileHandler fileHandler= ExecuteFileHandler.getInstance().
                            getHandlerByFile(file);*/
                    FileHandler fileHandler= ExecuteFileHandler.getInstance().getHandlerByFile(file);
                    if(ExecuteFileHandler.getInstance().isCompressFileHandler(fileHandler)){
                        bean.setIcoId(R.drawable.compressfileico);
                        bean.setCompressed(true);
                        bean.setCompressFileHandler(fileHandler);
                    }

                }
                String date_authrity=Utils.formateDate(file.lastModified(),Utils.DEFAULTDATEPATTERNNOTWITHMMSS);
                StringBuilder sb=new StringBuilder();

                sb.append(date_authrity).append(" ").append(bean.isDir() ? "d" : "f");
                sb.append(file.canWrite()?"w":"");
                sb.append(file.canRead()?"r":"");

                bean.setDate_authrity(sb.toString());
                list.add(bean);
                return true;
            }
        });


       /* operDir.listFiles(new FileFilter() {
            @Override
            public boolean accept(final File file) {

                FileInfoBean bean = new FileInfoBean();
                bean.setDir(file.isDirectory());
                bean.setFileName(file.getName());
                bean.setParentPath(Utils.normalizePath(file.getParent()));
                bean.setRootFile(operDir);
                bean.setLastModFileTime(file.lastModified());
                //todo 暂定
                if(bean.isDir()){
                    bean.setIcoId(R.drawable.diricocontent);
                          *//*  Runnable r= new Runnable(){
                                @Override
                                public void run() {
                                    float num=Utils.getFileSize(file);
                                }
                            };*//*
                    int numOfDirs= myDataList.getNumOfDirs()==-1?0:myDataList.getNumOfDirs();
                    myDataList.setNumOfDirs(++numOfDirs);
                    String[] files= file.list();
                    bean.setNumOfSubFile(files==null?0:files.length);
                }else{
                    int numOfFiles= myDataList.getNumOfFiles()==-1?0:myDataList.getNumOfFiles();
                    myDataList.setNumOfFiles(++numOfFiles);
                    bean.setSize(BigDecimal.valueOf(file.length()));
                    bean.setIcoId(R.drawable.fileico);

                    *//*FileHandler fileHandler= ExecuteFileHandler.getInstance().
                            getHandlerByFile(file);*//*

                    FileHandler fileHandler= ExecuteFileHandler.getInstance().getHandlerByFile(file);
                    if(ExecuteFileHandler.getInstance().isCompressFileHandler(fileHandler)){
                        bean.setIcoId(R.drawable.compressfileico);
                        bean.setCompressed(true);
                        bean.setCompressFileHandler(fileHandler);
                    }
                }
                String date_authrity=Utils.formateDate(file.lastModified(),Utils.DEFAULTDATEPATTERNNOTWITHMMSS);
                StringBuilder sb=new StringBuilder();

                sb.append(date_authrity).append(" ").append(bean.isDir() ? "d" : "f");
                sb.append(file.canWrite()?"w":"");
                sb.append(file.canRead()?"r":"");

                bean.setDate_authrity(sb.toString());
                list.add(bean);
                return true;
            }
        });*/
/*        float internal= Utils.getInternalAllSpace();
        float external= Utils.getExternalAllSpace();
        myDataList.setAllSpace(internal+external);*/
        myDataList.setParentPath(Utils.normalizePath(operDir.getAbsolutePath()));
        return myDataList;
    }

}
